package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.common.constants.WmNewsMessageConstants;
import com.heima.model.article.pojo.ApArticleConfig;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Component
public class ApArticleIsDownListener {

    @Autowired
    private ApArticleConfigMapper apArticleConfigMapper;

    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void listenerDownOrUp(String json){
        if (StringUtils.isNotBlank(json)){
            Map map = JSON.parseObject(json, Map.class);
            Long id = (Long) map.get("articleId");
            Integer status = (Integer) map.get("enable");
            LambdaQueryWrapper<ApArticleConfig> wrapper = new LambdaQueryWrapper<>();
            ApArticleConfig apArticleConfig = apArticleConfigMapper.selectOne(wrapper.eq(ApArticleConfig :: getArticleId, id));
            apArticleConfig.setIsDown(status == 0 ? true : false);
            apArticleConfigMapper.updateById(apArticleConfig);
        }

    }
}
